package com.sys.service;

import com.sys.GlobalVariable;
import com.sys.entity.Customer;
import com.sys.entity.Device;
import com.sys.exception.DuplicateCardException;
import com.sys.utils.DbUtil4;
import com.sys.utils.DbUtil4Factory;

import javax.servlet.http.Part;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DeviceService {
    public List<Map<String, Object>> showList(String dname, String istrouble, int uid) throws Exception {
        DbUtil4 db = null;
        List<Map<String, Object>> list = null;
        String sql = "select did,head,dname,setup_time,lastrepeat_time,fix_time,istrouble,create_time,modify_time,isdel,uid from t_device where  isdel=0 and uid=? "
                ;
        // 查询条件不确定，需要动态拼装sqllist = {ArrayList@3983}  size = 1
        // 动态添加参数，不能使用数组，因为数组定义好后，是固定长度
        List paramList = new ArrayList();
        paramList.add(uid);
        if(dname!=null && !"".equals(dname)){
            sql += " and dname like ?";
            paramList.add("%"+dname+"%");
        }
        if(istrouble!=null && !"".equals(istrouble) && !"-1".equals(istrouble)){
            sql += " and istrouble like ?";
            paramList.add(istrouble+"%");
        }
        sql += "order by modify_time desc,did";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            list = db.query(sql, paramList.toArray() );
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return list;
    }
    public static void add(String fileDB, String dname, String setup_time, String lastrepeat_time, String fix_time, String istrouble,Integer uid) throws Exception {
        DbUtil4 db = null;
        String selectSQL = "select count(1) as num from t_device where dname=? and setup_time=? and lastrepeat_time=?";
        String sql = "INSERT INTO t_device (head,dname,setup_time,lastrepeat_time,fix_time,istrouble,uid) VALUES (?,?,?,?,?,?,?)";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            List<Map<String, Object>> list = db.query(selectSQL, new Object[]{dname, setup_time, lastrepeat_time});
            // 由于查询的是 select count(1) ，所以一定有一个值
//            Map<String, Object> map = list.get(0);
//            Long num = (Long) map.get("num");
            // 将上面两行代码合成一行
            Long num = (Long)  list.get(0).get("num");
            if(num == 0){ // 没有重复的记录
                db.update(sql, new Object[]{fileDB,dname,setup_time,lastrepeat_time,fix_time,istrouble,uid});
            }else{ // 如果名片存在，抛出异常
                throw new DuplicateCardException("设备已经存在！");
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
    }
    public int delete(int did) throws Exception {
        DbUtil4 db = null;
        String sql = "UPDATE t_device SET isdel = 1 WHERE did = ?";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            int j = db.update(sql, new Object[]{did});
            return j;
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
    }
    public Device showById(String did) throws Exception{
        List<Device> list = null;
        DbUtil4 db = null;
        boolean t = false;
        String sql = "SELECT head,did,dname,setup_time,lastrepeat_time,fix_time,istrouble,create_time,modify_time,isdel,uid FROM t_device WHERE did = ? and isdel = 0";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            list = db.query(sql, new Object[]{did}, Device.class);
            if(list!=null && !list.isEmpty()){
                Device device = list.get(0);
                return device;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
        return null;
    }
    public int update(String head,String dname, String setup_time,String lastrepeat_time, String fix_time,String istrouble, int did) throws Exception {
        DbUtil4 db = null;
        String sql = "update t_device set head=?,dname=?,setup_time=?,lastrepeat_time=?,fix_time=?,istrouble=? where did=?";
        try {
            db = DbUtil4Factory.getDbUtil4(GlobalVariable.dbconfigPath, "utf-8");
            int i = db.update(sql, new Object[]{head,dname,setup_time,lastrepeat_time,fix_time,istrouble,did});
            return i;
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (SQLException e) {
                throw e;
            }
        }
    }
}
